package com.dianping.cat.consumer.event.model.transform;
import com.dianping.cat.consumer.event.model.IVisitor;
import com.dianping.cat.consumer.event.model.entity.EventName;
import com.dianping.cat.consumer.event.model.entity.EventReport;
import com.dianping.cat.consumer.event.model.entity.EventType;
import com.dianping.cat.consumer.event.model.entity.GraphTrend;
import com.dianping.cat.consumer.event.model.entity.Machine;
import com.dianping.cat.consumer.event.model.entity.Range;
import com.dianping.cat.consumer.event.model.entity.StatusCode;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

public class DefaultNativeParser implements IVisitor {
  private DefaultLinker m_linker;
  
  public DefaultNativeParser(InputStream in) {
/*  19 */     this.m_linker = new DefaultLinker(true);



    
    this.m_in = new DataInputStream(in);
  }
  private DataInputStream m_in;
  
/*  28 */   public static EventReport parse(byte[] data) { return parse(new ByteArrayInputStream(data)); }

  
  public static EventReport parse(InputStream in) {
    DefaultNativeParser parser = new DefaultNativeParser(in);
/*  33 */     EventReport eventReport = new EventReport();
    
    try {
      eventReport.accept(parser);
/*  37 */     } catch (RuntimeException e) {
/*  38 */       if (e.getCause() == null || !(e.getCause() instanceof java.io.EOFException))
      {
      
/*  41 */         throw e;
      }
    }
    
/*  45 */     parser.m_linker.finish();
/*  46 */     return eventReport;
  }


  
  public void visitEventReport(EventReport eventReport) {
    byte tag;
/*  53 */     if ((tag = readTag()) != -4) {
/*  54 */       throw new RuntimeException(String.format("Malformed payload, expected: %s, but was: %s!", new Object[] { Integer.valueOf(-4), Byte.valueOf(tag) }));
    }
    
/*  57 */     while ((tag = readTag()) != -1) {
/*  58 */       visitEventReportChildren(eventReport, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }
  
  protected void visitEventReportChildren(EventReport eventReport, int _field, int _type) {
/*  63 */     switch (_field) {
      case 1:
/*  65 */         eventReport.setDomain(readString());
        break;
      case 2:
/*  68 */         eventReport.setStartTime(readDate());
        break;
      case 3:
/*  71 */         eventReport.setEndTime(readDate());
        break;
      case 4:
/*  74 */         if (_type == 1) {
/*  75 */           eventReport.addDomain(readString()); break;
/*  76 */         }  if (_type == 2) {
/*  77 */           for (int i = readInt(); i > 0; i--) {
            eventReport.addDomain(readString());
          }
        }
        break;
      case 5:
/*  83 */         if (_type == 1) {
/*  84 */           eventReport.addIp(readString()); break;
/*  85 */         }  if (_type == 2) {
/*  86 */           for (int i = readInt(); i > 0; i--) {
/*  87 */             eventReport.addIp(readString());
          }
        }
        break;
      case 33:
/*  92 */         if (_type == 1) {
/*  93 */           Machine machine = new Machine();

/*  95 */           visitMachine(machine);
/*  96 */           this.m_linker.onMachine(eventReport, machine); break;
/*  97 */         }  if (_type == 2) {
/*  98 */           for (int i = readInt(); i > 0; i--) {
/*  99 */             Machine machine = new Machine();

/* 101 */             visitMachine(machine);
/* 102 */             this.m_linker.onMachine(eventReport, machine);
          }
        }
        break;
    }
  }


  
  public void visitGraphTrend(GraphTrend graphTrend) {
    byte tag;
/* 113 */     while ((tag = readTag()) != -1) {
/* 114 */       visitGraphTrendChildren(graphTrend, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }
  
  protected void visitGraphTrendChildren(GraphTrend graphTrend, int _field, int _type) {
/* 119 */     switch (_field) {
      case 1:
/* 121 */         graphTrend.setDuration(readInt());
        break;
      case 2:
/* 124 */         graphTrend.setCount(readString());
        break;
      case 3:
/* 127 */         graphTrend.setFails(readString());
        break;
    }
  }


  
  public void visitMachine(Machine machine) {
    byte tag;
/* 136 */     while ((tag = readTag()) != -1) {
/* 137 */       visitMachineChildren(machine, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }
  
  protected void visitMachineChildren(Machine machine, int _field, int _type) {
/* 142 */     switch (_field) {
      case 1:
/* 144 */         machine.setIp(readString());
        break;
      case 33:
/* 147 */         if (_type == 1) {
/* 148 */           EventType type = new EventType();

/* 150 */           visitType(type);
/* 151 */           this.m_linker.onType(machine, type); break;
/* 152 */         }  if (_type == 2) {
/* 153 */           for (int i = readInt(); i > 0; i--) {
/* 154 */             EventType type = new EventType();

/* 156 */             visitType(type);
/* 157 */             this.m_linker.onType(machine, type);
          }
        }
        break;
    }
  }


  
  public void visitName(EventName name) {
    byte tag;
/* 168 */     while ((tag = readTag()) != -1)
/* 169 */       visitNameChildren(name, (tag & 0xFF) >> 2, tag & 0x3); 
  }
  
  protected void visitNameChildren(EventName name, int _field, int _type) {
    GraphTrend graphTrend;
/* 174 */     switch (_field) {
      case 1:
/* 176 */         name.setId(readString());
        break;
      case 2:
/* 179 */         name.setTotalCount(readLong());
        break;
      case 3:
/* 182 */         name.setFailCount(readLong());
        break;
      case 4:
/* 185 */         name.setFailPercent(readDouble());
        break;
      case 5:
/* 188 */         name.setSuccessMessageUrl(readString());
        break;
      case 6:
/* 191 */         name.setFailMessageUrl(readString());
        break;
      case 7:
/* 194 */         name.setTps(readDouble());
        break;
      case 33:
/* 197 */         if (_type == 1) {
/* 198 */           Range range = new Range();

/* 200 */           visitRange(range);
/* 201 */           this.m_linker.onRange(name, range); break;
/* 202 */         }  if (_type == 2) {
/* 203 */           for (int i = readInt(); i > 0; i--) {
/* 204 */             Range range = new Range();

/* 206 */             visitRange(range);
/* 207 */             this.m_linker.onRange(name, range);
          }
        }
        break;
      case 34:
/* 212 */         graphTrend = new GraphTrend();

/* 214 */         visitGraphTrend(graphTrend);
/* 215 */         this.m_linker.onGraphTrend(name, graphTrend);
        break;
      case 35:
/* 218 */         if (_type == 1) {
/* 219 */           StatusCode statusCode = new StatusCode();

/* 221 */           visitStatusCode(statusCode);
/* 222 */           this.m_linker.onStatusCode(name, statusCode); break;
/* 223 */         }  if (_type == 2) {
/* 224 */           for (int i = readInt(); i > 0; i--) {
/* 225 */             StatusCode statusCode = new StatusCode();

/* 227 */             visitStatusCode(statusCode);
/* 228 */             this.m_linker.onStatusCode(name, statusCode);
          }
        }
        break;
    }
  }


  
  public void visitRange(Range range) {
    byte tag;
/* 239 */     while ((tag = readTag()) != -1) {
/* 240 */       visitRangeChildren(range, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }
  
  protected void visitRangeChildren(Range range, int _field, int _type) {
/* 245 */     switch (_field) {
      case 1:
/* 247 */         range.setValue(Integer.valueOf(readInt()));
        break;
      case 2:
/* 250 */         range.setCount(readInt());
        break;
      case 3:
/* 253 */         range.setFails(readInt());
        break;
    }
  }


  
  public void visitStatusCode(StatusCode statusCode) {
    byte tag;
/* 262 */     while ((tag = readTag()) != -1) {
/* 263 */       visitStatusCodeChildren(statusCode, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }
  
  protected void visitStatusCodeChildren(StatusCode statusCode, int _field, int _type) {
/* 268 */     switch (_field) {
      case 1:
/* 270 */         statusCode.setId(readString());
        break;
      case 2:
/* 273 */         statusCode.setCount(readLong());
        break;
    }
  }


  
  public void visitType(EventType type) {
    byte tag;
/* 282 */     while ((tag = readTag()) != -1)
/* 283 */       visitTypeChildren(type, (tag & 0xFF) >> 2, tag & 0x3); 
  }
  
  protected void visitTypeChildren(EventType type, int _field, int _type) {
    GraphTrend graphTrend;
/* 288 */     switch (_field) {
      case 1:
/* 290 */         type.setId(readString());
        break;
      case 2:
/* 293 */         type.setTotalCount(readLong());
        break;
      case 3:
/* 296 */         type.setFailCount(readLong());
        break;
      case 4:
/* 299 */         type.setFailPercent(readDouble());
        break;
      case 5:
/* 302 */         type.setSuccessMessageUrl(readString());
        break;
      case 6:
/* 305 */         type.setFailMessageUrl(readString());
        break;
      case 7:
/* 308 */         type.setTps(readDouble());
        break;
      case 33:
/* 311 */         if (_type == 1) {
/* 312 */           EventName name = new EventName();

/* 314 */           visitName(name);
/* 315 */           this.m_linker.onName(type, name); break;
/* 316 */         }  if (_type == 2) {
/* 317 */           for (int i = readInt(); i > 0; i--) {
/* 318 */             EventName name = new EventName();

/* 320 */             visitName(name);
/* 321 */             this.m_linker.onName(type, name);
          }
        }
        break;
      case 34:
/* 326 */         graphTrend = new GraphTrend();

/* 328 */         visitGraphTrend(graphTrend);
/* 329 */         this.m_linker.onGraphTrend(type, graphTrend);
        break;
    }
  }
  
  private Date readDate() {
    try {
/* 336 */       return new Date(readVarint(64));
/* 337 */     } catch (IOException e) {
/* 338 */       throw new RuntimeException(e);
    }
  }
  
  private double readDouble() {
    try {
/* 344 */       return Double.longBitsToDouble(readVarint(64));
/* 345 */     } catch (IOException e) {
/* 346 */       throw new RuntimeException(e);
    }
  }
  
  private int readInt() {
    try {
/* 352 */       return (int)readVarint(32);
/* 353 */     } catch (IOException e) {
/* 354 */       throw new RuntimeException(e);
    }
  }
  
  private long readLong() {
    try {
/* 360 */       return readVarint(64);
/* 361 */     } catch (IOException e) {
/* 362 */       throw new RuntimeException(e);
    }
  }
  
  private String readString() {
    try {
/* 368 */       return this.m_in.readUTF();
/* 369 */     } catch (IOException e) {
/* 370 */       throw new RuntimeException(e);
    }
  }
  
  private byte readTag() {
    try {
/* 376 */       return this.m_in.readByte();
/* 377 */     } catch (IOException e) {
/* 378 */       throw new RuntimeException(e);
    }
  }
  
  protected long readVarint(int length) throws IOException {
/* 383 */     int shift = 0;
/* 384 */     long result = 0L;

/* 386 */     while (shift < length) {
/* 387 */       byte b = this.m_in.readByte();
/* 388 */       result |= (b & 0x7F) << shift;
/* 389 */       if ((b & 0x80) == 0) {
/* 390 */         return result;
      }
/* 392 */       shift += 7;
    }
    
/* 395 */     throw new RuntimeException("Malformed variable int " + length + "!");
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-consumer-3.0.0.jar!/com/dianping/cat/consumer/event/model/transform/DefaultNativeParser.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */